package com.youtui.manager.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.youtui.common.constant.SystemConstant;
import com.youtui.common.service.base.impl.BaseServiceImpl;
import com.youtui.common.util.DateUtils;
import com.youtui.common.util.Md5Util;
import com.youtui.common.util.NextIdUtil;
import com.youtui.common.util.UuidUtil;
import com.youtui.manager.mapper.DeveloperMapper;
import com.youtui.manager.model.Account;
import com.youtui.manager.model.Developer;
import com.youtui.manager.model.DeveloperExample;
import com.youtui.manager.model.DeveloperExample.Criteria;
import com.youtui.manager.service.IAccountInfoService;
import com.youtui.manager.service.IAccountService;
import com.youtui.manager.service.IDeveloperService;

@Service
public class DeveloperServiceImpl extends BaseServiceImpl implements IDeveloperService {

	@Autowired
	private DeveloperMapper developerMapper;

	@Autowired
	private IAccountService accountService;

	@Autowired
	private IAccountInfoService accountInfoService;

	@Override
	public PageInfo<Developer> findDeveloperPagelist(Developer developer, int page, int rows) {

		PageHelper.startPage(page, rows);
		List<Developer> list = findDeveloperlist(developer);
		PageInfo<Developer> pageinfo = new PageInfo<>(list);
		return pageinfo;
	}

	@Override
	public List<Developer> findDeveloperlist(Developer developer) {

		DeveloperExample example = new DeveloperExample();
		Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(developer.getAppKey())) {
			criteria.andAppKeyEqualTo(developer.getAppKey());
		}
		if (StringUtils.isNotBlank(developer.getAppName())) {
			criteria.andAppNameLike("%" + developer.getAppName() + "%");
		}
		example.setOrderByClause(" id desc");
		List<Developer> list = developerMapper.selectByExample(example);
		return list;
	}

	@Override
	public Developer findDeveloperById(Long id) {
		if (id == null) {
			return null;
		}
		Developer dev = developerMapper.selectByPrimaryKey(id);
		return dev;
	}

	@Override
	public Developer findDeveloperByAppkey(String appkey) {
		DeveloperExample example = new DeveloperExample();
		Criteria criteria = example.createCriteria();
		criteria.andAppKeyEqualTo(appkey);
		List<Developer> list = developerMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Developer findDeveloperByUserName(String username) {
		DeveloperExample example = new DeveloperExample();
		Criteria criteria = example.createCriteria();
		criteria.andLoginNameEqualTo(username);
		
		Criteria criteriaor = example.or();
		criteriaor.andMobileEqualTo(username);
		
		List<Developer> list = developerMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Developer findDeveloperByLoginName(String loginName) {
		DeveloperExample example = new DeveloperExample();
		Criteria criteria = example.createCriteria();
		criteria.andLoginNameEqualTo(loginName);
 
		List<Developer> list = developerMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	@Override
	public Developer findDeveloperByMobile(String mobile) {
		DeveloperExample example = new DeveloperExample();
		Criteria criteria = example.createCriteria(); 
		criteria.andMobileEqualTo(mobile); 
		List<Developer> list = developerMapper.selectByExample(example);
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	@Override
	public Developer developerAdd(Developer developer) {
		if (developer.getId() != null) {
			return null;
		}
		//developer.setId(NextIdUtil.getNextId());
		developer.setPassword(Md5Util.md5(developer.getPassword()));
		developer.setAppKey(UuidUtil.getUuid());
		
		String appSecret = Md5Util.md5(developer.getAppKey()+DateUtils.getTimestamp());
		developer.setAppSecret(appSecret);
		
		developer.setChannelType(1);
		developer.setStatus(SystemConstant.status_effect);
		developer.setCreatetime(new Date());
		developerMapper.insert(developer);

		developer=findDeveloperByAppkey(developer.getAppKey());
		
		// 创建账户
		Account account = accountService.add(developer.getId());

		// 创建账户信息
		accountInfoService.add(developer.getId(), account.getId());

		return developer;
	}

	@Override
	public Developer update(Developer developer) {
		if (developer.getId() == null) {
			return null;
		}
		developerMapper.updateByPrimaryKeySelective(developer);
		return developer;
	}

	@Override
	public Developer clear(Developer developer) {
		if (developer != null) {
			developer.setPassword("");
			developer.setAppSecret("");
		}
		return developer;
	}

}
